<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isshowDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="obj">
                  <el-button size="small" type="success" @click="assignPerm(obj.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(obj.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(obj.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="pageConfing.pagesize" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog :title="roleForm.id?'编辑弹层':'新增角色'" :visible="isshowDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible="isShowPermission" @close="btnCancelPerm">
        <el-tree
          ref="permTree"
          :data="permList"
          :props="{label:'name'}"
          default-expand-all
          show-checkbox
          check-strictly
          node-key="id"
        />
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="btnOKPerm">确定</el-button>
            <el-button size="small" @click="btnCancelPerm">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getCompantInfo, getRoleList, delRole, addRole, getRoleDetail, editRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      roleId: '',
      permList: [],
      isShowPermission: false,
      companyInfo: {},
      roleList: [], // 继承数组
      pageConfing: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2
      },
      total: 0, // 记录总数
      isshowDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCompantInfo()
    this.getRoleList() // 获取角色列表
    this.getPermissionList() // 获取权限列表
  },
  methods: {
    // 权限弹窗
    async assignPerm(id) {
      const { permIds } = await getRoleDetail(id)
      this.roleId = id
      this.isShowPermission = true
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    async getPermissionList() {
      this.permList = listToTreeData(await getPermissionList(), '0')
    },
    // 关闭权限弹窗
    async btnOKPerm() {
      // 发请求
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowPermission = false
    },
    btnCancelPerm() {
      // 清理数据
      this.$refs.permTree.setCheckedKeys([])
      this.isShowPermission = false
    },
    // 关闭弹窗
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.isshowDialog = false
    },
    async btnOK() {
      // 校验
      await this.$refs.roleForm.validate()
      // 发送请求
      if (this.roleForm.id) {
        await editRole(this.roleForm)
      } else {
        await addRole(this.roleForm)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭窗口
      this.isshowDialog = false
      // 更新页面
      this.getRoleList()
    },
    async getCompantInfo() {
      this.companyInfo = await getCompantInfo(this.$store.state.user.userInfo.companyId)
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageConfing) // {rows} 结构
      // console.log(res)
      this.total = total
      this.roleList = rows
    },
    // 显示页码
    currentChange(newPage) {
      this.pageConfing.page = newPage
      this.getRoleList()
    },
    // 删除
    async delRole(id) {
      // 二次确认
      await this.$confirm('是否确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // 发请求
      await delRole(id)
      // 提示用户
      this.$message.success('删除成功')
      // 加载数据
      this.getRoleList()
    },
    // 编辑的回显
    async editRole(id) {
      // 回显
      this.roleForm = await getRoleDetail(id)
      // 弹窗
      this.isshowDialog = true
    }
  }
}
</script>

<style>

</style>
